package maven.demo5;

import java.util.InputMismatchException;
import java.util.Scanner;

public class TryCatchFinally {
    public void testDivider() {
        // demo1. try...catch... 小例子 --- 多重 catch 块
        Scanner input = new Scanner(System.in);
        try {
            System.out.println("请输入第一个数：");
            int one = input.nextInt();
            System.out.println("请输入第二个数：");
            int two = input.nextInt();
            System.out.println("两个数字相除：" + one / two);
        } catch (InputMismatchException e) {
            System.out.println("必须输入整数");
        } catch (ArithmeticException e) {
            System.out.println("除数不能为0");
        } catch (Exception e) {
            System.out.println("未知异常");
        }
    }


    /**
     * divider(除数)
     * result(结果)
     * try...catch...捕获 while 循环
     * @return
     */
    public int testCircle() {
        int divider = 10;
        int result = 0;
        try {
            while (divider > -1) {
                divider--;
                result = result + 100 / divider;
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("循环抛出异常了！！！");
            return -1;
        }
    }


    /**
     * divider(除数)
     * result(结果)
     * try...catch...finally捕获 while 循环
     * @return
     */
    public int testFinally() {
        int divider = 10;
        int result = 0;
        try {
            while (divider > -1) {
                divider--;
                result = result + 100 / divider;
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("循环抛出异常了！！！");
            return result = 999;
        } finally {
            // tips: 即使 try...catch... 中 return 值，finally 仍然会执行；
            System.out.println("这是finally!!");
            System.out.println("result的值是：" + result);
        }
    }


    /**
     * divider(除数)
     * result(结果)
     * try...catch...finally捕获 while 循环
     * @return
     */
    public int testCatchFinally() {
        int divider = 10;
        int result = 0;
        try {
            while (divider > -1) {
                divider--;
                result = result + 100 / divider;
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("循环抛出异常了！！！");
        } finally {
            // tips: 即使 try...catch... 中 return 值，finally 仍然会执行；
            System.out.println("这是finally!!");
            System.out.println("result的值是：" + result);
        }

        // tips: try...catch...finally 中，没有 return 时，会返回其他模块的 return 值
        return 111;
    }

    /**
     * 测试 try...catch...finally 中的 return
     * @return
     */
    public int testFinallyReturn() {
        int divider = 10;
        int result = 0;
        try {
            while (divider > -1) {
                divider--;
                result = result + 100 / divider;
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("循环抛出异常了！！！");
            return result = 999;
        } finally {
            // tips:
            //   1. 即使 try...catch... 中 return 值，finally 仍然会执行；
            //   2. finally 中有 return 时，finally 中的 return 值会作为函数的最终返回值;
            System.out.println("这是finally!!");
            System.out.println("result的值是：" + result);
            return 111;
        }
    }
}
